package com.mmdccj.yugu.controller;

import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.common.Result;
import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.entity.MusicLinkList;
import com.mmdccj.yugu.entity.MusicList;
import com.mmdccj.yugu.service.MusicListService;
import com.mmdccj.yugu.service.MusicService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMDCCJ
 * @date: 2023-11-28 00:00
 */
@Slf4j
@RestController
@RequestMapping("/musicList")
public class MusicListController {
    @Autowired
    private MusicService musicService;
    @Autowired
    private MusicListService musicListService;

    /***
     * 新建歌单
     *
     * @return Result<String> 返回结果
     */
    @PostMapping("/createList")
    public Result<String> createList(HttpSession session, String listTitle) {
        log.info("创建歌单...");
        String userId = (String) session.getAttribute("user");
        MusicList list = new MusicList();
        list.setCreatorId(userId);
        list.setTitle(listTitle);
        musicListService.insertMusicList(list);
        return Result.success("成功创建歌单:" + list.getTitle());
    }

    /**
     * 在歌单中添加音乐
     *
     * @param musicListId 歌单id
     * @param musicId     音乐id
     * @return Result<String>
     */
    @PostMapping("/addMusic")
    public Result<String> addMusic(String musicListId, String musicId) {
        log.info("在歌单 {} 中添加音乐 {} ", musicListId, musicId);
        // 判断音乐是否存在
        if (musicService.getMusicById(musicId) == null) {
            return Result.error("音乐不存在");
        }
        // 在连接表中建立连接
        musicListService.insertLink(musicListId, musicId);
        return Result.success("添加成功");
    }

    @PostMapping("/collectAllMusic")
    public Result<String> collectAllMusic(@RequestBody List<String> musicIds, String listId) {
        musicListService.collectAllMusic(musicIds, listId);
        return Result.success("成功", musicIds.get(0));
    }

    /***
     * 获取歌单
     * 暂未完成
     * @return Result<List < MusicList>>
     */
    // Todo 需要完善功能
    @GetMapping("/getList")
    public Result<List<MusicList>> getList() {
        return Result.success("成功获取", musicListService.selectMusicList());
    }

    /**
     * 获取歌单内歌曲数量
     */
    @GetMapping("/getMusicNum")
    public Result<Integer> getMusicNum(String musicListId) {
        return null;
    }

    /**
     * 获取Image
     *
     * @param musicListId 歌单id
     * @return Result<MusicList>
     */
    @GetMapping("/getImage")
    public Result<MusicList> getImage(String musicListId) {
        if (musicListId == null) {
            return Result.error("id为空");
        }
        return Result.success("成功", musicListService.getListImageFrom(musicListId));
    }

    /**
     * 更新歌单的图片url
     *
     * @param id  歌单id
     * @param src 新的图片
     * @return Result<String> 更新结果
     */
    @PostMapping("/updateImage")
    public Result<String> updateImage(String id, String src) {
        log.info("id:{},src:{}", id, src);
        musicListService.updateImage(src, id);
        return Result.success("成功");
    }

    /**
     * 待编辑歌单信息
     *
     * @param id 歌单id
     * @return Result<MusicList> 歌单信息
     */
    @GetMapping("/editInfo")
    public Result<MusicList> editInfo(String id) {
        if (id == null) {
            return Result.error("id为空");
        }
        return Result.success("成功", musicListService.editListInfo(id));
    }

    /**
     * 保存编辑后的信息
     *
     * @param id    歌单id
     * @param intro 描述
     * @param title 标题
     * @return Result<String> 结果
     */
    @PostMapping("/updateInfo")
    public Result<String> updateInfo(String id, String intro, String title) {
        musicListService.updateInfo(id, intro, title);
        return Result.success("成功");
    }

    /**
     * 获取用户所有的歌单
     *
     * @param session session
     * @return Result<List < MusicList>> 歌单列表
     */
    @GetMapping("/getUserMusicList")
    public Result<List<MusicList>> userMusicList(HttpSession session, String userId) {
        String id;
        if (userId == null) {
            if ((String) session.getAttribute("user") == null) {
                return Result.error("未登陆");
            } else {
                id = (String) session.getAttribute("user");
            }

        } else {
            id = userId;
        }
        return Result.success("成功", musicListService.getUserMusicList(id));
    }

    /**
     * 添加歌单到收藏
     *
     * @param session     session
     * @param musicListId 歌单id
     * @return Result<String> 结果
     */
    @PostMapping("/collectList")
    public Result<String> collectList(HttpSession session, String musicListId) {
        String userID = (String) session.getAttribute("user");
        if (userID == null) {
            return Result.error("未登录");
        }
        if (musicListService.checkCollected(userID, musicListId)) {
            return Result.error("已经收藏或为创建者");
        }
        if (!musicListService.isMusicList(musicListId)) {
            return Result.error("不存在该歌单");
        }
        if (musicListService.collectMusicList(userID, musicListId)) {
            return Result.success("成功");
        } else {
            return Result.error("已经收藏");
        }
    }

    /**
     * 获取用户收藏的全部歌单
     *
     * @param userId 用户id
     * @return Result<List < MusicList>>
     */
    @GetMapping("/getCollectList")
    public Result<List<MusicList>> getCollectList(String userId) {
        if (userId == null) {
            return Result.error("参数缺失");
        }
        return Result.success("成功", musicListService.getCollectList(userId));
    }

    /**
     * 分页方式获取全部歌单
     */
    @GetMapping("/getMusicList")
    public Result<PageInfo<MusicListDto>> getMusicList(Integer currentPage) {
        if (currentPage == null) {
            return Result.error("缺少参数");
        }

        return Result.success("成功", musicListService.getMusicList(currentPage));
    }

    /**
     * 分页方式推荐歌单
     *
     * @
     */
    @GetMapping("/getRecommendMusicList")
    public Result<PageInfo<MusicList>> getRecommendList(String id) {
        return Result.success("成功", musicListService.getRecommendList(id));
    }

    @GetMapping("/getCD")
    public Result<List<MusicList>> getCD() {
        return Result.success("成功", musicListService.get10CD());
    }

    @PostMapping("/uploadNewCD")
    public Result<String> uploadNewCD(HttpSession session, String title, String image, String intro) {
        if (title != null) {
            if (image == null) {
                image = "default-image.jpg";
            }
            if (intro == null) {
                intro = "暂无简介";
            }
            String userId = (String) session.getAttribute("user");
            if (userId == null) return Result.error("未登录");
            musicListService.insertCD(userId, title, image, intro);
            return Result.success("成功");
        } else {
            return Result.error("缺少参数");
        }
    }

    @GetMapping("/getUserCD")
    public Result<List<MusicList>> getUserCD(HttpSession session, String id) {
        String userId;
        if (id == null) {
            userId = (String) session.getAttribute("user");
            if (userId == null) {
                return Result.error("未登录");
            }
        } else {
            userId = id;
        }

        return Result.success("成功", musicListService.getUserCD(userId));
    }

    @GetMapping("/getCDEditInfo")
    public Result<MusicList> getCDEditInfo(HttpSession session, String id) {
        String userId = (String) session.getAttribute("user");
        if (id == null) {
            return Result.error("缺少参数");
        }
        if (userId == null) {
            return Result.error("未登录");
        }
        return Result.success("成功", musicListService.getCDEditInfo(userId, id));
    }

    @PostMapping("/updateCD")
    public Result<String> updateCD(HttpSession session, String id, String title, String introduce, String image) {
        String userId = (String) session.getAttribute("user");
        if (id == null || title == null || image == null) {
            return Result.error("缺少参数");
        }
        musicListService.updateCDInfo(userId, id, title, introduce, image);
        return Result.success("更新成功");
    }

    @PostMapping("/insertMusicToCD")
    public Result<String> updateCD(HttpSession session, @RequestBody List<MusicLinkList> info) {
        if (null == session.getAttribute("user")) {
            return Result.error("未登录");
        }

        musicListService.insertMusicToCD(info);
        return Result.success("成功");
    }

    @PostMapping("/deleteMusicFromCD")
    public Result<String> deleteMusicFromCD(HttpSession session, String listId, String musicId) {
        if (null == session.getAttribute("user")) {
            return Result.error("未登录");
        }
        musicListService.deleteMusicFromCD(listId, musicId);
        return Result.success("成功删除");
    }

    @PostMapping("/deleteCD")
    public Result<String> deleteCD(HttpSession session, String id) {
        String userId = (String) session.getAttribute("user");
        if (null == userId) {
            return Result.error("未登录");
        }
        if (id == null) {
            return Result.error("参数缺失");
        }
        musicListService.deleteCD(id, userId);
        return Result.success("成功删除");
    }
}
